﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using CompileApp.Models;

namespace CompileApp.windows;

public partial class Lr1Table : Window
{
    public Lr1Table(Parse parse)
    {
        InitializeComponent();

        List<LrTableData> list = new();

        LrGotoList.AutoGenerateColumns = false;
        LrGotoList.Columns.Add(new DataGridTextColumn() { Header = "状态", Width = 80, Binding = new Binding("state") });
        foreach (var nonTerminators in parse.NonTerminators)
        {
            if (nonTerminators.Equals("START"))
            {
                continue;
            }

            LrGotoList.Columns.Add(new DataGridTextColumn()
                { Header = nonTerminators, Width = 100, Binding = new Binding(nonTerminators) });
        }

        foreach (var key in parse.Significations.Keys)
        {
            var dict = new Dictionary<string, object>();
            dict.Add("state", Convert.ToInt32(key));
            foreach (var terminator in parse.NonTerminators)
            {
                string item = "";
                var tuple = new Tuple<string, string>(key, terminator);
                if (parse.Goto.ContainsKey(tuple))
                {
                    item = parse.Goto[tuple];
                }

                dict.Add(terminator, item);
            }

            list.Add(new LrTableData(dict));
        }

        LrGotoList.ItemsSource = list;

        List<LrTableData> list1 = new();

        LrActionList.AutoGenerateColumns = false;
        LrActionList.Columns.Add(new DataGridTextColumn()
            { Header = "状态", Width = 80, Binding = new Binding("state") });
        var set = new HashSet<string>(parse.Terminators);
        set.Add("#");
        foreach (var terminator in set)
        {
            string item = terminator;
            switch (terminator)
            {
                case "(":
                    item = "zkh";
                    break;
                case ")":
                    item = "ykh";
                    break;
                case "{":
                    item = "zhkh";
                    break;
                case "}":
                    item = "yhkh";
                    break;
                case ";":
                    item = "fh";
                    break;
                case "=":
                    item = "dy";
                    break;
            }

            LrActionList.Columns.Add(new DataGridTextColumn()
                { Header = terminator, Width = 100, Binding = new Binding($"{item}") });
        }


        foreach (var key in parse.Significations.Keys)
        {
            var dict = new Dictionary<string, object>();
            dict.Add("state", Convert.ToInt32(key));
            foreach (var terminator in set)
            {
                string item = terminator;
                switch (terminator)
                {
                    case "(":
                        item = "zkh";
                        break;
                    case ")":
                        item = "ykh";
                        break;
                    case "{":
                        item = "zhkh";
                        break;
                    case "}":
                        item = "yhkh";
                        break;
                    case ";":
                        item = "fh";
                        break;
                    case "=":
                        item = "dy";
                        break;
                }

                string it = "";
                var tuple = new Tuple<string, string>(key, terminator);
                if (parse.Action.ContainsKey(tuple))
                {
                    var t = parse.Action[tuple];
                    switch (t.GType)
                    {
                        case ActionType.Acc:
                            it += "acc";
                            break;
                        case ActionType.R:
                            it += "r";
                            break;
                        case ActionType.S:
                            it += "S";
                            break;
                        default:
                            it += "wrong";
                            break;
                    }

                    if (!t.State.Equals("-1"))
                    {
                        it += t.State;
                    }
                }

                dict.Add(item, it);
            }

            list1.Add(new LrTableData(dict));
        }

        LrActionList.ItemsSource = list1;


        var treeViewItem = new TreeViewItem() { Header = "所有状态" };

        for (int i = 0; i < parse.StateNum; i++)
        {
            // Lr1StateBox.AppendText($"状态{i}:\n");
            var it = new TreeViewItem() { Header = $"状态{i}:\n" };
            treeViewItem.Items.Add(it);
            var productions = parse.Significations[i.ToString()];
            string result = "";
            foreach (var production in productions)
            {
                result += $"{production.LeftItem} -> ";
                int t = 0;
                foreach (var str in production.RightItem)
                {
                    if (t == production.Count)
                    {
                        result += "·";
                    }

                    t++;
                    result += $"[{str}] ";
                }

                if (production.Count == production.RightItem.Count)
                {
                    result += "·";
                }

                result += ", ";
                foreach (var str in production.ForwardItem)
                {
                    result += $"{str}/";
                }
                result = result.Substring(0, result.Length - 1);

                result += "\n";
            }

            it.Items.Add(new TreeViewItem() { Header = result });
        }

        StateTree.Items.Add(treeViewItem);
        
        var treeViewItem1 = new TreeViewItem() { Header = "所有规则" };
        
        foreach (var key in parse.Rules.Keys)
        {
            var item = parse.Rules[key];
            var it = new TreeViewItem() { Header = $"规则{key}:" };
            treeViewItem1.Items.Add(it);
            var result = $"{item.LeftItem} -> ";
            foreach (var str in item.RightItem)
            {
                result += $"[{str}]——";
            }
            result += "\n";
            it.Items.Add(new TreeViewItem() { Header = result });
        }
        
        RuleTree.Items.Add(treeViewItem1);
        
        
    }
}